﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CustomSecurity;

namespace CustomControls
{
    public partial class FilterInFlowPanel : DevExpress.XtraEditors.TextEdit
    {
        public FilterInFlowPanel()
        {
            InitializeComponent();
            FilterType = FilterTypes.Contains;
            this.ForeColor = Color.FromArgb(200, 200, 200);
            CaseSensitive = true;
        }
        public FlowLayoutPanel LayoutToFilter { get; set; }
        public FilterTypes FilterType { get; set; }
        public bool CaseSensitive { set; get; }
        public string WaterMark { get; set; }
        bool _isWaterMarkShowing=true;

        protected override void OnLoaded()
        {
            Text = WaterMark;
            base.OnLoaded();
        }
        protected override void OnEditorEnter(EventArgs e)
        {
            if (_isWaterMarkShowing)
            {
                this.Text = "";
                _isWaterMarkShowing = false;
                this.ForeColor = Color.Black;
            }
            base.OnEditorEnter(e);
        }
        protected override void OnEditorLeave(EventArgs e)
        {
            if (this.Text.Trim() == "")
            {
                this.Text = WaterMark;
                _isWaterMarkShowing = true;
                this.ForeColor = Color.FromArgb(200,200,200);
            }
            base.OnEditorLeave(e);
        }

        protected override void OnEditValueChanged()
        {
            if (LayoutToFilter == null) return;
            if (_isWaterMarkShowing )
            {
                foreach (Control child in LayoutToFilter.Controls)
                    if (User.CurUser.GetRight((string)child.Tag) != CustomSecurity.Right.None)
                        child.Visible = true;
            }
            else
            {
                this.ForeColor = Color.Black;
                switch (FilterType)
                {
                    case FilterTypes.Contains:
                        if (CaseSensitive)
                        {
                            foreach (Control child in LayoutToFilter.Controls)
                                if (User.CurUser.GetRight((string)child.Tag) != CustomSecurity.Right.None)
                                    child.Visible = child.Text.Contains(Text);
                        }
                        else
                        {
                            foreach (Control child in LayoutToFilter.Controls)
                                if (User.CurUser.GetRight((string)child.Tag) != CustomSecurity.Right.None)
                                    child.Visible = child.Text.ToLower().Contains(Text.ToLower());
                        }
                        break;
                    case FilterTypes.StartWith:
                        if (CaseSensitive)
                        {
                            foreach (Control child in LayoutToFilter.Controls)
                                if (User.CurUser.GetRight((string)child.Tag) != CustomSecurity.Right.None)
                                    child.Visible = child.Text.StartsWith(Text);
                        }
                        else
                        {
                            foreach (Control child in LayoutToFilter.Controls)
                                if (User.CurUser.GetRight((string)child.Tag) != CustomSecurity.Right.None)
                                    child.Visible = child.Text.ToLower().StartsWith(Text.ToLower());
                        }
                        break;
                    case FilterTypes.EndWith:
                        if (CaseSensitive)
                        {
                            foreach (Control child in LayoutToFilter.Controls)
                                if (User.CurUser.GetRight((string)child.Tag) != CustomSecurity.Right.None)
                                    child.Visible = child.Text.EndsWith(Text);
                        }
                        else
                        {
                            foreach (Control child in LayoutToFilter.Controls)
                                if (User.CurUser.GetRight((string)child.Tag) != CustomSecurity.Right.None)
                                    child.Visible = child.Text.ToLower().EndsWith(Text.ToLower());
                        }
                        break;
                }
            }
            base.OnEditValueChanged();
        }
    }
    public enum FilterTypes
    {
        Contains=1,
        StartWith=2,
        EndWith=3
    }
    
}
